﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControlsLib.Filters;
using GestionePolveriLib.BL.FilterBags;
using GestionePolveriLib.BL;
using GestionePolveriLib.Utilities;
using GestionePolveriLib.Facades;

namespace GestionePolveri2.Filters
{
    public partial class VerniciatoreScortaMinimaFilter : VerniciatoreScortaMinimaFilterBase
    {
        public VerniciatoreScortaMinimaFilter()
        {
            InitializeComponent();

            if (!DesignMode)
            {
                this.EnableDatesChooser = false;

                RefreshData();

                this.FromDate = DateTime.Now;
                this.ToDate = DateTime.Now;

                this.rbPolveriAll.Tag = PolvereTypes.Tutte;
                this.rbPolveriNormali.Tag = PolvereTypes.Normali;
                this.rbPolveriXXXX.Tag = PolvereTypes.XXXX;

                this.rbLottiAll.Tag = LottoTypes.Tutti;
                this.rbLottiNonNulli.Tag = LottoTypes.NonNulli;
                this.rbLottiNulli.Tag = LottoTypes.Nulli;

                this.rbPolveriAll.Checked = true;
                this.rbLottiNonNulli.Checked = true;
            }
        }

        protected override void OnResetFilter(EventArgs e)
        {
            FilterBag.CodicePolvere = null;
            FilterBag.Fornitore = null;
            FilterBag.Tipo = PolvereTypes.Tutte;
            FilterBag.Famiglia = null;
            FilterBag.ColoriAbbinati = null;
            FilterBag.TipologiaSM = null;
            FilterBag.StatoPolvere = PolvereStatus.Tutte.ToString();

            this.cmbFornitori.SelectedIndex = -1;
            this.cmbPolveri.SelectedIndex = -1;
            this.cmbColori.SelectedIndex = -1;
            this.chkCmbFamiglia.DeselectAll();
            this.cmbTipologiaSM.SelectedIndex = -1;
            this.chkCmbEditStatoPolvere.DeselectAll();
            this.chkCmbEditStatoPolvere.SetEditValue(null);

            this.rbPolveriAll.Checked = true;
            this.rbLottiNonNulli.Checked = true;

            base.OnResetFilter(e);
        }

        protected override void OnExecuteFilter(EventArgs e)
        {
            //if (this.cmbPolveri.SelectedItem == null)
            //    this.cmbPolveri.SelectedIndex = -1;
            //if (this.cmbFornitori.SelectedItem == null)
            //    this.cmbFornitori.SelectedIndex = -1;
            //if (this.cmbColori.SelectedItem == null)
            //    this.cmbColori.SelectedIndex = -1;

            Polvere polvereSelected = FilterComboBoxHelper.GetSelectedItem<Polvere>(this.cmbPolveri);
            Colore coloreSelected = FilterComboBoxHelper.GetSelectedItem<Colore>(this.cmbColori);
            Fornitore fornitoreSelected = FilterComboBoxHelper.GetSelectedItem<Fornitore>(this.cmbFornitori);
            TipologiaScortaMinima tipologiaSMSelected = FilterComboBoxHelper.GetSelectedItem<TipologiaScortaMinima>(this.cmbTipologiaSM);

            FilterBag.ColoriAbbinati = (coloreSelected == null ? null : ((Colore)this.cmbColori.SelectedItem).CodiceColore);
            FilterBag.Famiglia = (this.chkCmbFamiglia.Properties.GetCheckedItems().ToString() == string.Empty ? null : this.chkCmbFamiglia.Properties.GetCheckedItems().ToString());

            FilterBag.StatoPolvere = (this.chkCmbEditStatoPolvere.Properties.GetCheckedItems().ToString() == string.Empty ? PolvereStatus.Tutte.ToString() : this.chkCmbEditStatoPolvere.Properties.GetCheckedItems().ToString());

            FilterBag.Tipo = (PolvereTypes)(rbPolveriAll.Checked ? rbPolveriAll.Tag : (rbPolveriNormali.Checked ? rbPolveriNormali.Tag : rbPolveriXXXX.Tag));
            FilterBag.TipoLotto = (LottoTypes)(rbLottiAll.Checked ? rbLottiAll.Tag : (rbLottiNonNulli.Checked ? rbLottiNonNulli.Tag : rbLottiNulli.Tag));
            FilterBag.CodicePolvere = (polvereSelected == null ? null : ((Polvere)this.cmbPolveri.SelectedItem).CodicePolvere);
            FilterBag.Fornitore = (fornitoreSelected == null ? null : ((Fornitore)this.cmbFornitori.SelectedItem).Descrizione);
            FilterBag.TipologiaSM = (tipologiaSMSelected == null ? null : tipologiaSMSelected.Codice);

            FilterBag.PeriodFrom = this.FromDate;
            FilterBag.PeriodTo = this.ToDate;
            FilterBag.UseDates = this.UseDatesFilter;

            if(FilterBag.Famiglia != null)
              FilterBag.Famiglia = FilterBag.Famiglia.Replace(" ", "");

            if (
                (this.cmbFornitori.SelectedIndex == -1) &&
                (this.cmbPolveri.SelectedIndex == -1) &&
                (this.cmbTipologiaSM.SelectedIndex == -1) &&
                (FilterBag.Tipo == PolvereTypes.Tutte) &&
                (FilterBag.TipoLotto == LottoTypes.NonNulli) &&
                (FilterBag.Fornitore == null) &&
                (FilterBag.CodicePolvere == null) &&
                (FilterBag.UseDates == false) &&
                (FilterBag.ColoriAbbinati == string.Empty) &&
                (FilterBag.Famiglia == string.Empty) 
              )
            {
                OnResetFilter(e);
                this.OnFilterButtonClick();
            }
            else
            {
                base.OnExecuteFilter(e);
            }

            //base.OnExecuteFilter(e);
        }

        private void LoadPolveri()
        {
            FilterComboBoxHelper.SetupPolveriComboBox(this.cmbPolveri);
        }

        private void LoadFornitori()
        {
            FilterComboBoxHelper.SetupFornitoriComboBox(this.cmbFornitori);
        }

        private void LoadColori()
        {
            FilterComboBoxHelper.SetupColoriComboBox(this.cmbColori);
        }

        private void LoadTipologiaSM()
        {
            FilterComboBoxHelper.SetupTipologieSMPComboBox(this.cmbTipologiaSM);
        }

        private void LoadFamiglie()
        {
            FamigliaColoriFacade pf = new FamigliaColoriFacade();

            pf.RunAsync(() => pf.GetAll(), (result) =>
            {
                if (result != null)
                {
                    this.chkCmbFamiglia.Properties.Items.Clear();

                    this.chkCmbFamiglia.Properties.DisplayMember = "Descrizione";
                    this.chkCmbFamiglia.Properties.ValueMember = "ID";
                    this.chkCmbFamiglia.Properties.DataSource = result;
                }
            });
        }

        private void LoadStatiPolvere()
        {
            List<ControlsLib.Controls.ColorPicker.ColorInfo> statiPolvere = new List<ControlsLib.Controls.ColorPicker.ColorInfo>();
            statiPolvere.Add(new ControlsLib.Controls.ColorPicker.ColorInfo("Sotto Scorta", Color.Red));
            statiPolvere.Add(new ControlsLib.Controls.ColorPicker.ColorInfo("In Riordino", Color.Yellow));
            statiPolvere.Add(new ControlsLib.Controls.ColorPicker.ColorInfo("Generato Da Correzione", Color.Orange));

            //this.chkCmbEditStatoPolvere.Properties.DisplayMember = "Text";
            //this.chkCmbEditStatoPolvere.Properties.ValueMember = "Text";
            this.chkCmbEditStatoPolvere.Properties.DataSource = statiPolvere;
        }

        public override void RefreshData()
        {
            LoadPolveri();
            LoadFornitori();
            LoadColori();
            LoadFamiglie();
            LoadTipologiaSM();
            LoadStatiPolvere();
        }

        private void checkedComboBoxEdit1_Popup(object sender, EventArgs e)
        {
            DevExpress.XtraEditors.CheckedListBoxControl listBox =
                (sender as DevExpress.Utils.Win.IPopupControl).PopupWindow.Controls[3].Controls[0] as DevExpress.XtraEditors.CheckedListBoxControl;
            listBox.DrawItem += new DevExpress.XtraEditors.ListBoxDrawItemEventHandler(listBox_DrawItem);
        }

        void listBox_DrawItem(object sender, DevExpress.XtraEditors.ListBoxDrawItemEventArgs e)
        {
            ControlsLib.Controls.ColorPicker.ColorInfo p = e.Item as ControlsLib.Controls.ColorPicker.ColorInfo;
            if (p == null) 
            {
                e.Handled = false;
                return;
            }

            DevExpress.XtraEditors.CheckedListBoxControl checkList = sender as DevExpress.XtraEditors.CheckedListBoxControl;
            DevExpress.Utils.Drawing.CheckObjectPainter painter = DevExpress.Utils.Drawing.CheckPainterHelper.GetPainter(checkList.LookAndFeel);
            DevExpress.Utils.Drawing.CheckObjectInfoArgs args = new DevExpress.Utils.Drawing.CheckObjectInfoArgs(e.Cache, null);

            args.GlyphRect = new Rectangle(e.Bounds.Location, new Size(e.Bounds.Height, e.Bounds.Height));
            args.CheckState = checkList.GetItemCheckState(e.Index);

            // Draw color box
            Rectangle rectC = new Rectangle();
            rectC.X = args.GlyphRect.Width + e.Bounds.X + 2;
            rectC.Y = e.Bounds.Y + 2;
            rectC.Width = 18;
            rectC.Height = e.Bounds.Height - 5;
            //e.Graphics.FillRectangle(new SolidBrush(p.Color), rectC);
            //e.Graphics.DrawRectangle(SystemPens.WindowText, rectC);

            Point newP = new Point(e.Bounds.Location.X + rectC.X, e.Bounds.Location.Y + ((e.Bounds.Height - Font.Height) / 2));

            Rectangle rect = e.Bounds;
            rect.Offset(args.GlyphRect.Width + rectC.X + 4, 0);
            //drawing background
            e.Cache.FillRectangle(e.Appearance.GetBackBrush(e.Cache), e.Bounds);
            //drawing check box
            painter.DrawObject(args);

            // drawing color state box...
            e.Cache.FillRectangle(new SolidBrush(p.Color), rectC);
            e.Cache.DrawRectangle(SystemPens.WindowText, rectC);

            //drawing string
            e.Cache.DrawString(p.Text, e.Appearance.Font, new SolidBrush(Color.Black), rect, e.Appearance.GetStringFormat());
            DrawItemState state = DrawItemState.Focus | DrawItemState.Selected;
            //drawing focus rectangle
            if (e.State == state)
            {
                ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds, e.Appearance.GetForeColor(), e.Appearance.GetBackColor());
            }
            e.Handled = true;

        }
    }

    public class VerniciatoreScortaMinimaFilterBase : FilterContainerBase2<VerniciatoreScortaMinimaFilterBag> { }
}
